﻿using System.Collections.Generic;
using System.Globalization;

namespace HB.Expand
{
    public static class HexString2Object
    {
        /// <summary>
        /// 将16进制字符串转为byte
        /// </summary>
        /// <param name="s"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool GetByteFromHex(this string s, out byte data)
        {
            return byte.TryParse(s, NumberStyles.HexNumber, null, out data);
        }
        /// <summary>
        /// 将16进制字符串转为ushort
        /// </summary>
        /// <param name="s"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool GetUshortFromHex(this string s, out ushort data)
        {
            return ushort.TryParse(s, NumberStyles.HexNumber, null, out data);
        }
        /// <summary>
        /// 将16进制字符串转为uint
        /// </summary>
        /// <param name="s"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool GetUintFromHex(this string s, out uint data)
        {
            return uint.TryParse(s, NumberStyles.HexNumber, null, out data);
        }
        /// <summary>
        /// 将16进制字符串转为ulong
        /// </summary>
        /// <param name="s"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool GetUlongFromHex(this string s, out ulong data)
        {
            return ulong.TryParse(s, NumberStyles.HexNumber, null, out data);
        }
        /// <summary>
        /// 将16进制字符串数据转为原始字节数组
        /// </summary>
        /// <param name="s">如：AA 01 93 40 55 b1 f4</param>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static bool GetByteListFromHex(this string s, char splitor, out byte[] arr)
        {
            arr = null;
            if (s == null) { return false; }
            string[] liststr = s.Split(splitor);
            List<byte> lsbyte = new List<byte>();
            bool Result = true;
            foreach (var str in liststr)
            {
                if (str == "") { continue; }
                if (str.GetByteFromHex(out byte data))
                {
                    lsbyte.Add(data);
                }
                else
                {
                    Result = false;
                    break;
                }
            }
            if (Result)
            {
                arr = lsbyte.ToArray();
            }
            return Result;
        }
        /// <summary>
        /// 将16进制字符串数据转为原始字节数组
        /// </summary>
        /// <param name="s">如：AA01934055b1f4，无分隔的字符串，偶数长度</param>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static bool GetByteListFromHex(this string s, out byte[] arr)
        {
            arr = null;
            if (s.Length % 2 != 0) { return false; }
            List<byte> lsbyte = new List<byte>();
            bool Result = true;
            for (int i = 0; i < s.Length; i += 2)
            {
                if (s.Substring(i, 2).GetByteFromHex(out byte data))
                {
                    lsbyte.Add(data);
                }
                else
                {
                    Result = false;
                    break;
                }
            }
           
            if (Result)
            {
                arr = lsbyte.ToArray();
            }
            return Result;
        }
        
    }
}
